home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / HYP / T-Z / XCMDInfo.cpt / XCmdGlue.inc < prev    next >
Text File  |  1989-02-26  |  11KB  |  436 lines

  1. { XCmdGlue.inc -- Sample glue routines to call back to HyperCard.  
  2.   See example use in Peek.p and Flash.p
  3.   By Dan Winkler.  DO NOT call the author!  Contact Apple Developer 
  4.   Support on AppleLink "MacDst" or on MCI "MacTech".
  5.  
  6.       ©Apple Computer, Inc. 1987
  7.     All Rights Reserved.
  8. }
  9.  
  10. { The Pascal code for the XCMD or XFCN should include HyperXCmd.p at
  11.   the beginning and this file at the end.  There must be a variable 
  12.   named "paramPtr" that is the argument that was passed into 
  13.   the XCMD or XFCN. All strings are Pascal strings unless noted as
  14.   zero-terminated strings (no length byte and the string goes until a
  15.   zero byte is encountered). }
  16.  
  17.  
  18. PROCEDURE DoJsr(addr: ProcPtr); INLINE $205F,$4E90;
  19. { Jump subroutine to a procedure.   Pop address into A0, JSR (A0) }
  20.  
  21.  
  22. PROCEDURE SendCardMessage(msg: Str255);
  23. {  Send a HyperCard message (a command with arguments) to the current card. }
  24. BEGIN
  25.   WITH paramPtr^ DO
  26.     BEGIN
  27.       inArgs[1] := ORD(@msg);
  28.       request := xreqSendCardMessage;
  29.       DoJsr(entryPoint);
  30.     END;
  31. END;
  32.  
  33.  
  34. FUNCTION EvalExpr(expr: Str255): Handle;
  35. {  Evaluate a HyperCard expression and return the answer.  The answer is
  36.    a handle to a zero-terminated string. }
  37. BEGIN
  38.   WITH paramPtr^ DO
  39.     BEGIN
  40.       inArgs[1] := ORD(@expr);
  41.       request := xreqEvalExpr;
  42.       DoJsr(entryPoint);
  43.       EvalExpr := Handle(outArgs[1]);
  44.     END;
  45. END;
  46.  
  47.  
  48. FUNCTION StringLength(strPtr: Ptr): LongInt;
  49. {  Count the characters from where strPtr points until the next zero byte. 
  50.    Does not count the zero itself.  strPtr must be a zero-terminated string.  }
  51. BEGIN
  52.   WITH paramPtr^ DO
  53.     BEGIN
  54.       inArgs[1] := ORD(strPtr);
  55.       request := xreqStringLength;
  56.       DoJsr(entryPoint);
  57.       StringLength := outArgs[1];
  58.     END;
  59. END;
  60.  
  61.  
  62. FUNCTION StringMatch(pattern: Str255; target: Ptr): Ptr;
  63. { Perform case-insensitive match looking for pattern anywhere in
  64.   target, returning a pointer to first character of the first match,
  65.   in target or NIL if no match found.  pattern is a Pascal string,
  66.   and target is a zero-terminated string. }
  67. BEGIN
  68.   WITH paramPtr^ DO
  69.     BEGIN
  70.       inArgs[1] := ORD(@pattern);
  71.       inArgs[2] := ORD(target);
  72.       request := xreqStringMatch;
  73.       DoJsr(entryPoint);
  74.       StringMatch := Ptr(outArgs[1]);
  75.     END;
  76. END;
  77.  
  78.  
  79. PROCEDURE ZeroBytes(dstPtr: Ptr; longCount: LongInt);
  80. {  Write zeros into memory starting at destPtr and going for longCount 
  81.    number of bytes. }
  82. BEGIN
  83.   WITH paramPtr^ DO
  84.     BEGIN
  85.       inArgs[1] := ORD(dstPtr);
  86.       inArgs[2] := longCount;
  87.       request := xreqZeroBytes;
  88.       DoJsr(entryPoint);
  89.     END;
  90. END;
  91.  
  92.  
  93. FUNCTION PasToZero(str: Str255): Handle;
  94. {  Convert a Pascal string to a zero-terminated string.  Returns a handle
  95.    to a new zero-terminated string.  The caller must dispose the handle.
  96.    You'll need to do this for any result or argument you send from 
  97.    your XCMD to HyperTalk. }
  98. BEGIN
  99.   WITH paramPtr^ DO
  100.     BEGIN
  101.       inArgs[1] := ORD(@str);
  102.       request := xreqPasToZero;
  103.       DoJsr(entryPoint);
  104.       PasToZero := Handle(outArgs[1]);
  105.     END;
  106. END;
  107.  
  108.  
  109. PROCEDURE ZeroToPas(zeroStr: Ptr; VAR pasStr: Str255);
  110. {  Fill the Pascal string with the contents of the zero-terminated
  111.    string.  You create the Pascal string and pass it in as a VAR 
  112.    parameter.  Useful for converting the arguments of any XCMD to 
  113.    Pascal strings.}
  114. BEGIN
  115.   WITH paramPtr^ DO
  116.     BEGIN
  117.       inArgs[1] := ORD(zeroStr);
  118.       inArgs[2] := ORD(@pasStr);
  119.       request := xreqZeroToPas;
  120.       DoJsr(entryPoint);
  121.     END;
  122. END;
  123.  
  124.  
  125. FUNCTION StrToLong(str: Str31): LongInt;
  126. {  Convert a string of ASCII decimal digits to an unsigned long integer. }
  127. BEGIN
  128.   WITH paramPtr^ DO
  129.     BEGIN
  130.       inArgs[1] := ORD(@str);
  131.       request := xreqStrToLong;
  132.       DoJsr(entryPoint);
  133.       StrToLong := outArgs[1];
  134.     END;
  135. END;
  136.  
  137.  
  138. FUNCTION StrToNum(str: Str31): LongInt;
  139. {  Convert a string of ASCII decimal digits to a signed long integer.
  140.    Negative sign is allowed.  }
  141. BEGIN
  142.   WITH paramPtr^ DO
  143.     BEGIN
  144.       inArgs[1] := ORD(@str);
  145.       request := xreqStrToNum;
  146.       DoJsr(entryPoint);
  147.       StrToNum := outArgs[1];
  148.     END;
  149. END;
  150.  
  151.  
  152. FUNCTION StrToBool(str: Str31): BOOLEAN;
  153. {  Convert the Pascal strings 'true' and 'false' to booleans. }
  154. BEGIN
  155.   WITH paramPtr^ DO
  156.     BEGIN
  157.       inArgs[1] := ORD(@str);
  158.       request := xreqStrToBool;
  159.       DoJsr(entryPoint);
  160.       StrToBool := BOOLEAN(outArgs[1]);
  161.     END;
  162. END;
  163.  
  164.  
  165. FUNCTION StrToExt(str: Str31): Extended;
  166. {  Convert a string of ASCII decimal digits to an extended long integer. }
  167. VAR x: Extended;
  168. BEGIN
  169.   WITH paramPtr^ DO
  170.     BEGIN
  171.       inArgs[1] := ORD(@str);
  172.       inArgs[2] := ORD(@x);
  173.       request := xreqStrToExt;
  174.       DoJsr(entryPoint);
  175.       StrToExt := x;
  176.     END;
  177. END;
  178.  
  179.  
  180. FUNCTION LongToStr(posNum: LongInt): Str31;
  181. {  Convert an unsigned long integer to a Pascal string.  }
  182. VAR str: Str31;
  183. BEGIN
  184.   WITH paramPtr^ DO
  185.     BEGIN
  186.       inArgs[1] := posNum;
  187.       inArgs[2] := ORD(@str);
  188.       request := xreqLongToStr;
  189.       DoJsr(entryPoint);
  190.       LongToStr := str;
  191.     END;
  192. END;
  193.  
  194.  
  195. FUNCTION NumToStr(num: LongInt): Str31;
  196. {  Convert a signed long integer to a Pascal string.  }
  197. VAR str: Str31;
  198. BEGIN
  199.   WITH paramPtr^ DO
  200.     BEGIN
  201.       inArgs[1] := num;
  202.       inArgs[2] := ORD(@str);
  203.       request := xreqNumToStr;
  204.       DoJsr(entryPoint);
  205.       NumToStr := str;
  206.     END;
  207. END;
  208.  
  209.  
  210. FUNCTION NumToHex(num: LongInt; nDigits: INTEGER): Str31;
  211. {  Convert an unsigned long integer to a hexadecimal number and put it
  212.    into a Pascal string.  }
  213. VAR str: Str31;
  214. BEGIN
  215.   WITH paramPtr^ DO
  216.     BEGIN
  217.       inArgs[1] := num;
  218.       inArgs[2] := nDigits;
  219.       inArgs[3] := ORD(@str);
  220.       request := xreqNumToHex;
  221.       DoJsr(entryPoint);
  222.       NumToHex := str;
  223.     END;
  224. END;
  225.  
  226.  
  227. FUNCTION BoolToStr(bool: BOOLEAN): Str31;
  228. {  Convert a boolean to 'true' or 'false'.  }
  229. VAR str: Str31;
  230. BEGIN
  231.   WITH paramPtr^ DO
  232.     BEGIN
  233.       inArgs[1] := LongInt(bool);
  234.       inArgs[2] := ORD(@str);
  235.       request := xreqBoolToStr;
  236.       DoJsr(entryPoint);
  237.       BoolToStr := str;
  238.     END;
  239. END;
  240.  
  241.  
  242. FUNCTION ExtToStr(num: Extended): Str31;
  243. {  Convert an extended long integer to decimal digits in a string.  }
  244. VAR str: Str31;
  245. BEGIN
  246.   WITH paramPtr^ DO
  247.     BEGIN
  248.       inArgs[1] := ORD(@num);
  249.       inArgs[2] := ORD(@str);
  250.       request := xreqExtToStr;
  251.       DoJsr(entryPoint);
  252.       ExtToStr := str;
  253.     END;
  254. END;
  255.  
  256.  
  257. FUNCTION GetGlobal(globName: Str255): Handle;
  258. {  Return a handle to a zero-terminated string containing the value of 
  259.    the specified HyperTalk global variable.  }
  260. BEGIN
  261.   WITH paramPtr^ DO
  262.     BEGIN
  263.       inArgs[1] := ORD(@globName);
  264.       request := xreqGetGlobal;
  265.       DoJsr(entryPoint);
  266.       GetGlobal := Handle(outArgs[1]);
  267.     END;
  268. END;
  269.  
  270.  
  271. PROCEDURE SetGlobal(globName: Str255; globValue: Handle);
  272. {  Set the value of the specified HyperTalk global variable to be
  273.    the zero-terminated string in globValue.  The contents of the 
  274.    Handle are copied, so you must still dispose it afterwards.  }
  275. BEGIN
  276.   WITH paramPtr^ DO
  277.     BEGIN
  278.       inArgs[1] := ORD(@globName);
  279.       inArgs[2] := ORD(globValue);
  280.       request := xreqSetGlobal;
  281.       DoJsr(entryPoint);
  282.     END;
  283. END;
  284.  
  285.  
  286. FUNCTION GetFieldByName(cardFieldFlag: BOOLEAN; fieldName: Str255): Handle;
  287. {  Return a handle to a zero-terminated string containing the value of 
  288.    field fieldName on the current card.  You must dispose the handle.  }
  289. BEGIN
  290.   WITH paramPtr^ DO
  291.     BEGIN
  292.       inArgs[1] := ORD(cardFieldFlag);
  293.       inArgs[2] := ORD(@fieldName);
  294.       request := xreqGetFieldByName;
  295.       DoJsr(entryPoint);
  296.       GetFieldByName := Handle(outArgs[1]);
  297.     END;
  298. END;
  299.  
  300.  
  301. FUNCTION GetFieldByNum(cardFieldFlag: BOOLEAN; fieldNum: INTEGER): Handle;
  302. {  Return a handle to a zero-terminated string containing the value of 
  303.    field fieldNum on the current card.  You must dispose the handle.  }
  304. BEGIN
  305.   WITH paramPtr^ DO
  306.     BEGIN
  307.       inArgs[1] := ORD(cardFieldFlag);
  308.       inArgs[2] := fieldNum;
  309.       request := xreqGetFieldByNum;
  310.       DoJsr(entryPoint);
  311.       GetFieldByNum := Handle(outArgs[1]);
  312.     END;
  313. END;
  314.  
  315.  
  316. FUNCTION GetFieldByID(cardFieldFlag: BOOLEAN; fieldID: INTEGER): Handle;
  317. {  Return a handle to a zero-terminated string containing the value of 
  318.    the field whise ID is fieldID.  You must dispose the handle.  }
  319. BEGIN
  320.   WITH paramPtr^ DO
  321.     BEGIN
  322.       inArgs[1] := ORD(cardFieldFlag);
  323.       inArgs[2] := fieldID;
  324.       request := xreqGetFieldByID;
  325.       DoJsr(entryPoint);
  326.       GetFieldByID := Handle(outArgs[1]);
  327.     END;
  328. END;
  329.  
  330.  
  331. PROCEDURE SetFieldByName(cardFieldFlag: BOOLEAN; fieldName: Str255; fieldVal: Handle);
  332. {  Set the value of field fieldName to be the zero-terminated string 
  333.    in fieldVal.  The contents of the Handle are copied, so you must 
  334.    still dispose it afterwards.  }
  335. BEGIN
  336.   WITH paramPtr^ DO
  337.     BEGIN
  338.       inArgs[1] := ORD(cardFieldFlag);
  339.       inArgs[2] := ORD(@fieldName);
  340.       inArgs[3] := ORD(fieldVal);
  341.       request := xreqSetFieldByName;
  342.       DoJsr(entryPoint);
  343.     END;
  344. END;
  345.  
  346.  
  347. PROCEDURE SetFieldByNum(cardFieldFlag: BOOLEAN; fieldNum: INTEGER; fieldVal: Handle);
  348. {  Set the value of field fieldNum to be the zero-terminated string 
  349.    in fieldVal.  The contents of the Handle are copied, so you must 
  350.    still dispose it afterwards.  }
  351. BEGIN
  352.   WITH paramPtr^ DO
  353.     BEGIN
  354.       inArgs[1] := ORD(cardFieldFlag);
  355.       inArgs[2] := fieldNum;
  356.       inArgs[3] := ORD(fieldVal);
  357.       request := xreqSetFieldByNum;
  358.       DoJsr(entryPoint);
  359.     END;
  360. END;
  361.  
  362.  
  363. PROCEDURE SetFieldByID(cardFieldFlag: BOOLEAN; fieldID: INTEGER; fieldVal: Handle);
  364. {  Set the value of the field whose ID is fieldID to be the zero-
  365.    terminated string in fieldVal.  The contents of the Handle are 
  366.    copied, so you must still dispose it afterwards.  }
  367. BEGIN
  368.   WITH paramPtr^ DO
  369.     BEGIN
  370.       inArgs[1] := ORD(cardFieldFlag);
  371.       inArgs[2] := fieldID;
  372.       inArgs[3] := ORD(fieldVal);
  373.       request := xreqSetFieldByID;
  374.       DoJsr(entryPoint);
  375.     END;
  376. END;
  377.  
  378.  
  379. FUNCTION StringEqual(str1,str2: Str255): BOOLEAN;
  380. {  Return true if the two strings have the same characters.  
  381.    Case insensitive compare of the strings.  }
  382. BEGIN
  383.   WITH paramPtr^ DO
  384.     BEGIN
  385.       inArgs[1] := ORD(@str1);
  386.       inArgs[2] := ORD(@str2);
  387.       request := xreqStringEqual;
  388.       DoJsr(entryPoint);
  389.       StringEqual := BOOLEAN(outArgs[1]);
  390.     END;
  391. END;
  392.  
  393.  
  394. PROCEDURE ReturnToPas(zeroStr: Ptr; VAR pasStr: Str255);
  395. {  zeroStr points into a zero-terminated string.  Collect the 
  396.    characters from there to the next carriage Return and return 
  397.    them in the Pascal string pasStr.  If a Return is not found, 
  398.    collect chars until the end of the string. }
  399. BEGIN
  400.   WITH paramPtr^ DO
  401.     BEGIN
  402.       inArgs[1] := ORD(zeroStr);
  403.       inArgs[2] := ORD(@pasStr);
  404.       request := xreqReturnToPas;
  405.       DoJsr(entryPoint);
  406.     END;
  407. END;
  408.  
  409.  
  410. PROCEDURE ScanToReturn(VAR scanPtr: Ptr);
  411. {  Move the pointer scanPtr along a zero-terminated 
  412.    string until it points at a Return character
  413.    or a zero byte.  }
  414. BEGIN
  415.   WITH paramPtr^ DO
  416.     BEGIN
  417.       inArgs[1] := ORD(@scanPtr);
  418.       request := xreqScanToReturn;
  419.       DoJsr(entryPoint);
  420.     END;
  421. END;
  422.  
  423.  
  424. PROCEDURE ScanToZero(VAR scanPtr: Ptr);
  425. {  Move the pointer scanPtr along a zero-terminated 
  426.    string until it points at a zero byte.  }
  427. BEGIN
  428.   WITH paramPtr^ DO
  429.     BEGIN
  430.       inArgs[1] := ORD(@scanPtr);
  431.       request := xreqScanToZero;
  432.       DoJsr(entryPoint);
  433.     END;
  434. END;
  435.  
  436.